
( 'hello world' delog .)

( '------------- class test -------------' delog .)

const (: 'myclass' ( class [
  has (: 'm' 5 )
  has (: 'n' 7 )
  does (: 'calc' ( number ) [
    this n + ( this m ) * ( that )
  ] )
  does (: '+' ( myclass ) [
    this
    ( this n ( + ( that n ) !) .)
    ( this m ( + ( that m ) !) .)
  ] )
  has (: 's' 'asdf' )
  gets (: ( number ) [ this ( 'hellow numbr' delog ( that delog .) .) ] )
  gets (: ( myclass ) [ this ( 'got a myclass' delog .) ] )
] ) )

var (: 'myobj-1' ( myclass new ) )
var (: 'myobj-2' ( myclass new ) )

( myobj-2 ( m ( 1 !) .) ( n ( 2 !) .) calc 10 delog .)

( myobj-1 + ( myobj-2 ) ( m delog .) ( n delog .) .)

( myobj-1 calc 10 delog .)



( '------------- gets context test ---------------' delog .)

( myobj-1 ( myobj-2 ) ( myobj-2 ) ( myobj-2 ) .)

( myobj-1 1 2 3 .)


( '------------- scope test -------------' delog .)

fun (: 'tf' [
  ( that > 0 else [ return ( none ) ] .)
  ( const (: 'a' ( that ) ) .)
  ( a delog .)
  ( tf ( that - 1 ) .)
  ( a delog .)
] )

( tf 3 .)

( '------------- recursive loop test -------------' delog .)

fun (: 'recgen' [
  ( that throw .)
  that > 0 then [ recgen ( that - 1 ) ]
] )

fun (: 'recprint' [
  that next delog then [ recprint ( that ) ]
] )

( recprint ( generator (: ( number ) [ recgen 5 ] ) each ) .)


( '------------- endless loop test -------------' delog .)

fun (: 'range' ( class [ none param (: 'to' (number) 0 ) param (: 'from' (number) 0 ) ] ) [
  var (: 'index' ( that from ) )
  const (: 'stop-range' ( that to ) )
  loop [
    ( index ( + 1 !) < ( stop-range ) else [ stop ] .)
    ( index throw .)
  ]
] )

var (: 'elements1' ( generator (: ( number ) [ range (: -3 3 ) ] ) each ) )

loop [
  elements1 next delog else [ stop ]
]

( '-------------- loop test -------------' delog .)


var (: 'elements2' ( generator (: ( number ) [ range (: 0 6 ) ] ) each ) )
var (: 'sum' 0 )
loop [
  sum ( + ( elements2 next delog else [ stop ] ) !)
]
( sum delog .)


( '------------- sort test -------------' delog .)


fun (: 'print-all' ( set ) [
  const (: 'elements' ( that each ) )
  ( 'printing all' delog .)
  loop [
    ( elements next else [ return ( none ) ] delog .)
  ]
  that
] )


fun (: 'sort' ( set ) [

  const (: 'elements' ( that each ) )
  const (: 'pivot' ( elements next else [ return [] ] ) )
  var (: 'low' [] )
  var (: 'high' [] )

  loop [
    elements next else [ stop ] > ( pivot )
    then [ high ( elements value ) ]
    else [ low ( elements value ) ]
  ]

  sort( low ) ( pivot ) merge( sort( high ) )

] )


const (: 'l' [ 3 8 5 1 ] )

( print-all( l ) .)

( print-all( sort( l ) delog ) .)

( '------------- function test -------------' delog .)

fun (: 'ftest' ( number ) [
  ( 'function testing test' `	delog .)
  ( this delog .)
  ( that delog .)
  'returnvalue'
] )

fun (: 'ftest2' ( list ) [
  ( 'function testing test 2' delog .)
  ( this delog .)
  ( print-all ( that ) .)
  ( print-all ( sort ( that ) ) .)
  'returnvalue'
] )

fun (: 'ftest3' ( class [ none param (: 'p1' (number) 0 ) param (: 'p2' (number) 0 ) ] ) [
  ( 'function testing test 3' delog .)
  ( this delog .)
  ( that p1 delog .)
  ( that p2 delog .)
  that p1 + ( that p2 ) / 2
] )



( ftest 34 delog .)

( ftest2 ( l ) .)

( ftest2 (: 10 4 11 4 ) .)

( ftest3 (: 10 20 ) delog .)

var (: 'ftest4' ( ftest3 delog ) delog )

( ftest4 (: 20 30 ) delog .)

var (: 'ftest5' ( myobj-1 calc ) )

( ftest5 10 delog .)

var (: 'ftest6' ( function (: ( number ) [
  ( 'function testing test 6' delog .)
  ( this delog .)
  ( that delog .)
  'returnvalue'
] ) ) )

( 'using function' delog .)
( ftest6 235 delog .)

( '------------- defs test -------------' delog .)

( myobj-1 calc 10 delog .)

( myobj-1 ( defs (: 'ps' [
  this calc 55
] ) !) delog .)

( myobj-1 calc 10 delog .)

( myobj-1 ps delog .)

( '------------- def test -------------' delog .)

def (: 'ok' [ 'yes' ] )

( ok delog .)

( '------------- finnished -------------' delog .)

( print-all ( list ( number ) merge [ 1 2 3 ] ) .)

( print-all ( [ 'a' 1 'b' 2 'c' 3 ] merge [ 5 6 'dgsfgdfsg' 5 ] ) .)

( print-all ( [ 'a' 1 'b' 2 'c' 3 ] merge ( [] ( 100 + 1 ) ( 100 + 2 ) ( 100 + 3 ) ) ) .)

var (: 'l1' [] )
( l1 ( 23 ) .)
( l1 ( 23 ) .)
( l1 ( 23 ) .)
( print-all ( l1 ( 1 + 2 ) ( 3 + 4 ) ) .)

( [] each next delog .)


( '------------- list test -------------' delog .)

var (: 'alist' ( list ( number ) 1 2 3 ) )
var (: 'blist' ( list ( number ) 4 5 6 ) )

( print-all ( alist ) .)
( print-all ( blist ) .)

( alist ( merge ( blist ) !) .)

( print-all ( alist 24 ) .)

( print-all ( sort ( generator (: ( number ) [ range (: -3 3 ) ] ) ) ) .)

( '------------- vector test -------------' delog .)

const (: 'vector' (
  class [
    has (: 'x' 0 )
    has (: 'y' 0 )
    does (: '+' ( vector ) [
      ( 'adding vectors' delog .)
      ( this x ( + ( that x ) !) .)
      ( this y ( + ( that y ) !) .)
    ] )
  ]
  is (
    function (: ( class [ none param (: 'y' (number) 0 ) param (: 'x' (number) 0 ) ] ) [
      vector new ( x ( that x !) .) ( y ( that y !) .)
    ] )
  )
) )

var (: 'v1' ( vector (: 7 7 ) ) )
var (: 'v2' ( vector (: 3 4 ) ) )

( v2 + ( v1 ) .)

( v2 x delog .)
( v2 y delog .)

( '------------- function passing test -------------' delog .)

fun (: 'ftest10' ( function ) [
  ( 'function testing test 10' delog .)
  that 11
] )

( ftest10 ( function (: ( number ) [ that * 9 ] ) ) delog .)

( '------------- apply test -------------' delog .)

const (: 'ftest11' ( ftest apply 100 ) )

( ftest11 delog 10 .)

( '------------- generator test -------------' delog .)

var (: 'gen1' ( generator (: ( number ) [ ( 11 throw .) ( 33 throw .) ( 22 throw .) ] ) ) )

var (: 'list1' [ 1000 2 ] )


( gen1 delog .)

( print-all ( sort( gen1 ) ) .)

( print-all ( sort( list1 merge ( gen1 ) ) ( length delog .) ) .)


( '------------- catch/throw test -------------' delog .)

catch (: ( number ) [ 7878 ( 'catch number' delog .) ( that delog .) ] )

( function (: ( number ) [
  that * 100 ( 5757575 throw .)
] ) 99 delog .)

( 4646464 throw delog .)

( '------------- finnished -------------' delog .)




